<?php

// require __DIR__ . '/vendor/autoload.php';
ini_set('display_errors', 1); //错误信息
ini_set('display_startup_errors', 1); //php启动错误信息
error_reporting(-1);
include_once 'simplehtmldom_1_9_1/simple_html_dom.php';
// include_once 'tabletojson/src/HTMLTable2JSON2.php';

$marc_no  = $_REQUEST['marc_no'];
$htmlPage = file_get_html('http://210.28.96.53/opac/item.php?marc_no=' . $marc_no);

$info = [];
foreach ($htmlPage->find('.booklist') as $item) {

    $dt = $item->find('dt', 0)->innertext;

    $dd1 = $item->find('dd', 0)->innertext;
    $dd2 = $item->find('dd', 0)->plaintext;
    $dd  = $item->find('dd', 0)->outertext;
    if (strlen(trim($dd2)) > 0) {
        $info[] = ["key" => $dt, "value" => $dd2];
    }
    // echo $dt, ": innertext: ", $dd1, "<br /> outertext: ", $dd, "<p /> plaintext dd2:", $dd2, "<p />";
}

$table_header = [];
$table_body   = [];
$table        = $htmlPage->find('#tabs2 table', 0);

$trs           = $table->find('tr');
$is_first_line = true;
$j             = 0;
foreach ($trs as $tr) {
    $i   = 0;
    $tds = $tr->find('td');
    foreach ($tds as $td) {
        if ($j == 0) {
            $table_header[$i] = trim($td->plaintext);
            $is_first_line    = false;
        } else {
            $table_body[$j][$i] = trim($td->plaintext);
        }
        $i++;
    }
    $j++;
}
$rs         = [];
$rs['info'] = $info;
// echo "\n<br />ttc1:", $t;
// var_export($table_header);
// var_export($table_body);
$rs['header'] = $table_header;
$rs['body']   = $table_body;
// $helper = new HTMLTable2JSON();

// // Standard Usage
// $helper->tableToJSON($t);
// echo "\n<br />", $helper;
$htmlPage->clear();
unset($htmlPage);
echo json_encode($rs, JSON_UNESCAPED_UNICODE);
